package com.juvepoland.jpdroid.activities;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.json.JSONException;

import com.juvepoland.jpdroid.R;
import com.juvepoland.jpdroid.application.JPdroidApplication;
import com.juvepoland.jpdroid.common.JPdroidSecurity;
import com.juvepoland.jpdroid.managers.impl.UserManager;
import com.juvepoland.jpdroid.models.User;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends BasicActivity {

	Button loginButton;
	EditText loginEditText;
	EditText passwordEditText;
	CheckBox autoLoginCheckbox;
	UserManager userManager = new UserManager();
	private SharedPreferences sharedPreferences;
	
	private TextView loginDisplayTextView;
	private TextView usernameLabelTextView;
	private TextView passwordLabelTextView;
	private TextView autologinTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_page_layout);
		
		loginButton = (Button) findViewById(R.id.login_button);
		loginButton.setTypeface(basicFontTypeFace);
		loginEditText = (EditText) findViewById(R.id.input_login);
		loginEditText.setTypeface(basicFontTypeFace);
		passwordEditText = (EditText) findViewById(R.id.password_input);
		passwordEditText.setTypeface(basicFontTypeFace);
		autoLoginCheckbox = (CheckBox) findViewById(R.id.autologin_checkbox);
		loginDisplayTextView = (TextView) findViewById(R.id.login_display_text);
		usernameLabelTextView = (TextView) findViewById(R.id.login_text);
		passwordLabelTextView = (TextView) findViewById(R.id.password_text);
		autologinTextView = (TextView) findViewById(R.id.autologin_text);
		
		usernameLabelTextView.setTypeface(basicFontTypeFace);
		passwordLabelTextView.setTypeface(basicFontTypeFace);
		loginDisplayTextView.setTypeface(basicFontTypeFace);
		autologinTextView.setTypeface(basicFontTypeFace);
		
		sharedPreferences = getSharedPreferences(MY_PREFERENCES, Activity.MODE_PRIVATE);
		
		addLoginButtonOnClickListener();
	}

	private void addLoginButtonOnClickListener() {
		loginButton.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
							new AsyncActionClass().execute(null);
						}
		});
	}

	protected boolean validate() {
		if(loginEditText.getText() == null || loginEditText.getText().toString().trim().length()==0){
			Toast.makeText(getApplicationContext(), "Błąd! Nie wpisano loginu!", Toast.LENGTH_LONG).show();
			return false;
		}
		if(passwordEditText.getText() == null || passwordEditText.getText().toString().trim().length()==0){
			Toast.makeText(getApplicationContext(), "Błąd! Nie wpisano hasła!", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	@Override
	protected void doInBackgroundAsyncProcess() throws Exception {
		if(validate()){
			User user = userManager.getUser(loginEditText.getText().toString().trim(), JPdroidSecurity.toMD5(passwordEditText.getText().toString().trim()));
			if(user!=null){
				if(autoLoginCheckbox.isChecked()){
					saveUserAndPassword();
				}
				Toast.makeText(getApplicationContext(), "Witaj, "+user.getUsername(), Toast.LENGTH_LONG).show();
				JPdroidApplication app = (JPdroidApplication)getApplicationContext();
				app.setLoggedUser(user);
				finish();
			}
		}
		
	}
	
	

	protected void saveUserAndPassword() {
		SharedPreferences.Editor preferencesEditor = sharedPreferences.edit();
		preferencesEditor.putString("username", loginEditText.getText().toString());
		try {
			preferencesEditor.putString("password", JPdroidSecurity.toMD5(passwordEditText.getText().toString()));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		preferencesEditor.commit();
	}

	@Override
	protected Intent getIntentToReturnAfterLogout() {
		return null;
	}


	
	

}
